package com.exam.dao.grade;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.DateType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.ExamStuVO;
import com.exam.common.vo.GradeVO;
import com.exam.common.vo.page.PageModel;
import com.exam.common.vo.page.SystemContext;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.ExamGrade;

@Repository("examGradeDaoImp")
public class ExamGradeDaoImp extends BaseDaoImpl<ExamGrade> implements ExamGradeDao{

	@Override
	public void updateGrade(Integer subjectId, Long stuId,Integer point) {
		String hql = " insert into ExamGrade(examGradePoint) "
				+ "value (:point) "
				+ "where examStuId = :stuId and examSubjectId = :subjectId";
		this.getSession().createQuery(hql)
		.setParameter("point", point)
		.setParameter("stuId", stuId)
		.setParameter("subjectId", subjectId)
		.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public PageModel<GradeVO> gradeList(ExamStuVO stuVO) {
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		PageModel<GradeVO> page = new PageModel<GradeVO>();
		
		StringBuffer sb = new StringBuffer();
		sb.append("select gd.exam_grade_id as examGradeId,gd.exam_stu_id as examStuId,gd.exam_grade_id as examSubjectId, ");
		sb.append("gd.exam_grade_point as examGradePoint,gd.exam_grade_right as examGradeRight,");
		sb.append("gd.exam_grade_sum as examGradeSum,gd.exam_grade_time as examGradeTime,");
		sb.append("gd.exam_grade_flag as examGradeFlag,");
		sb.append("st.exam_stu_name as examStuName,sj.exam_subject_name as examSubjectName,");
		sb.append("sj.exam_subject_totaltime as examSubjectTotaltime ");
		sb.append("from exam_grade as gd left join exam_stu as st on gd.exam_stu_id=st.exam_stu_id ");
		sb.append("left join exam_subject as sj on sj.exam_subject_id=gd.exam_subject_id");
		
		if(stuVO !=null){
			sb.append(" and gd.exam_stu_id=:stuId");
		}
		Query query = null;
		try {
			query = this.getSession().createSQLQuery(sb.toString())
					.addScalar("examGradeId",IntegerType.INSTANCE)
					.addScalar("examStuId",IntegerType.INSTANCE)
					.addScalar("examSubjectId",IntegerType.INSTANCE)
					.addScalar("examGradePoint",IntegerType.INSTANCE)
					.addScalar("examGradeRight",IntegerType.INSTANCE)
					.addScalar("examGradeSum",IntegerType.INSTANCE)
					.addScalar("examGradeTime",DateType.INSTANCE)
					.addScalar("examGradeFlag",IntegerType.INSTANCE)
					.addScalar("examStuName",StringType.INSTANCE)
					.addScalar("examSubjectName",StringType.INSTANCE)
					.addScalar("examSubjectTotaltime",IntegerType.INSTANCE)
					.setResultTransformer(Transformers.aliasToBean(GradeVO.class));
		} catch (HibernateException e1) {
			e1.printStackTrace();
		}
		
		if(stuVO !=null){
			query.setParameter("stuId", stuVO.getExamStuId());
		}
		Long totalSize = null;
				
		List<GradeVO> datas = new ArrayList<GradeVO>();
		try {
			totalSize = (long) query.list().size();
			datas = query.setFirstResult(offset).setMaxResults(pageSize).list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		
		
		page.setOffset(offset);
		page.setPageSize(pageSize);
		page.setDatas(datas);
		page.setTotalSize(totalSize);
		
		return page;
	}

}
